package com.googlecode.cnasort.cc.rmi.client.core;

import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.rmi.RemoteException;
import org.apache.log4j.Logger;
import com.googlecode.cnasort.cc.rmi.common.RMIFileLoaderConnector;
import com.googlecode.cnasort.cc.rmi.common.data.RMIDirectory;
import com.googlecode.cnasort.cc.rmi.common.data.RMIFile;
import com.googlecode.cnasort.cc.rmi.common.data.impl.RMIFileLoaderIdBean;

public class FileLoaderClientCoreImpl implements FileLoaderClientCore
{
    private static final Logger logger = Logger.getLogger( FileLoaderClientCoreImpl.class );

    private RMIFileLoaderIdBean id;
    private RMIFileLoaderConnector fileLoaderConnector;
    private PrintStream out;
    private File outputDirFile;
    private FileLoaderClientCoreFilters filters;

    public FileLoaderClientCoreImpl(
        RMIFileLoaderConnector  fileLoaderConnector,
        PrintStream             out,
        File                    outputDirFile,
        FileLoaderClientCoreFilters filers
        ) throws RemoteException
    {
        this.id = new RMIFileLoaderIdBean();

        if( fileLoaderConnector.connect( id ) ) {
            this.fileLoaderConnector    = fileLoaderConnector;
            this.out                    = out;
            this.outputDirFile          = outputDirFile;
            this.filters                = filers;
            }
        else {
            logger.warn( "Connection not allowed" );

            throw new SecurityException();
            }
    }

    public File getOutputDirFile()
    {
        return outputDirFile;
    }

    @Override
	public void doQuit() throws RemoteException
    {
        fileLoaderConnector.quit();
    }

    @Override
	public void doDownload( final String prefixToDownload ) throws IOException
    {
        new FileLoaderClientDownload(this, prefixToDownload).doDownload();
    }

    @Override
	public void printDirectoriesAndFiles() throws RemoteException
    {
        printDirectoriesAndFiles( this.fileLoaderConnector.getRootDirectories( id ) );
    }

    private void printDirectoriesAndFiles( final RMIDirectory[] folders ) throws RemoteException
    {
        for( RMIDirectory folder : folders ) {
            if( this.filters.getDirectoryFilter().accept( folder ) ) {
                out.println( "D:" + folder );

                RMIDirectory[] subFolders  = getDirectories( folder );

                printDirectoriesAndFiles( subFolders );

                RMIFile[] files = getFiles( folder );

                for( RMIFile file : files ) {
                    if( this.filters.getFileFilter().accept( file ) ) {
                        out.println( "F:" + file );
                        }
                    }
                }
            }
    }

//    public RMIFileLoaderConnector getRMIFileLoaderConnector()
//    {
//        return fileLoaderConnector;
//    }

    public RMIDirectory[] getDirectories( RMIDirectory folder ) throws RemoteException
    {
        return fileLoaderConnector.getDirectories( id, folder );
    }

    public RMIFile[] getFiles( RMIDirectory folder ) throws RemoteException
    {
        return fileLoaderConnector.getFiles( id, folder );
    }

    public void openFileTransfert( RMIFile file ) throws RemoteException
    {
        fileLoaderConnector.openFileTransfert( id, file );
    }

    public boolean isFileTransfertComplete() throws RemoteException
    {
        return fileLoaderConnector.isFileTransfertComplete( id );
    }

    public byte[] nextFileTransfertPart() throws RemoteException
    {
        return fileLoaderConnector.nextFileTransfertPart( id );
    }

    public void closeFileTransfert() throws RemoteException
    {
        fileLoaderConnector.closeFileTransfert( id );
    }
}
